package com.threegroup.web.controller;

import com.threegroup.common.constant.Constant;
import com.threegroup.common.dto.ShoppingCartDTO;
import com.threegroup.common.utils.Result;
import com.threegroup.common.utils.UserUtils;
import com.threegroup.web.annotation.Login;
import com.threegroup.web.pojo.ShoppingCartEntity;
import com.threegroup.web.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/mp/shoppingCart")
public class ShoppingCartController {

    @Resource
    private UserUtils userUtils;

    @Resource
    private ShoppingCartService shoppingCartService;

    @Login
    @RequestMapping("list")
    public Result<List<ShoppingCartEntity>> getShoppingCartList(HttpServletRequest request) {
        //获取用户id
        Long userId = userUtils.getUserId();
        String id = String.valueOf(userId);


        Result<List<ShoppingCartEntity>> listResult = shoppingCartService.getShoppingCartList(id);
        return listResult;
    }

    @Login
    @PostMapping("add")
    public Result addShopping(@RequestBody ShoppingCartDTO shoppingCartDTO, HttpServletRequest request) {

        //获取当前用户Id
        Long userId = userUtils.getUserId();
        String id = String.valueOf(userId);

        Result<ShoppingCartDTO> result = shoppingCartService.addShoppingCart(id,shoppingCartDTO);
        return result;
    }

    @Login
    @PostMapping("sub")
    public Result<ShoppingCartDTO> subShopping(@RequestBody ShoppingCartDTO shoppingCartDTO, HttpServletRequest request){

        //获取当前用户Id
        Long userId = userUtils.getUserId();
        String id = String.valueOf(userId);


        Result<ShoppingCartDTO> result = shoppingCartService.subShoppingCart(id,shoppingCartDTO);
        return result;

    }

    @Login
    @DeleteMapping("clean")
    public Result cleanShopping(HttpServletRequest request){

        //获取当前用户Id
        Long userId = userUtils.getUserId();
        String id = String.valueOf(userId);

        shoppingCartService.deleteShopping(id);
        return new Result();

    }
}
